package com.nulldev.util.web.HttpServer3.extensions.nCaptcha.noscript_frontend;

import com.nulldev.util.io.file.FileUtil;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.user.User;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.util.UUIDCookieLoader;
import com.nulldev.util.web.HttpServer3.legacyPages.v1.RequestType;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;

public class ChallengeIFramePage extends HttpPage3 {

	private static final String PAYLOAD = FileUtil.getResourceAsString(NoScriptFrontendResource.class, "challenge.html");

	public ChallengeIFramePage() {
		super("/captcha-legacy");
	}

	@SuppressWarnings("resource")
	@Override
	public Response onRequest(final Request request) {
		if (request.getType() != RequestType.GET)
			return new Response(400);

		final User user = UUIDCookieLoader.tryUserFromRequest(request);
		if (user == null) {
			return new Response(403);
		}

		return new Response(200, createStream(PAYLOAD)).header("Set-Cookie", UUIDCookieLoader.makeCookieFromUUID(user));
	}
}
